import BasePanel from "./BasePanel";
import CommonConstant from "../../constant/CommonConstant";
import EnumData from "../../Enum/EnumData";
import { GamePropData } from "../Data/GamePropData";
import { utils } from "../../utils/CommonUtil";
import RendererGameProp from "../Camera/RendererGameProp";
import GameConfig from "../../GameConfig";
import CameraRayLogic from "../Camera/CameraRayLogic";

export default class PropMessagePanel extends BasePanel {

    /** 当前的道具数据*/
    private curGamePropData: GamePropData;

    //propImage根物体
    private propImageRoot: fairygui.GGroup;
    /** 道具图片*/
    private propImage: fairygui.GLoader;
    /** 图片名*/
    private imageText: fairygui.GLabel;

    private okText: fairygui.GLabel;

    // //rawImage根物体
    // private rawImageRoot: fairygui.GGroup;
    /** 3D物体渲染图片*/
    private render3DTexture: fairygui.GImage;
    /** 3D物体渲染的物体的 名字*/
    private renderText: fairygui.GLabel;

    private renderCameraPrefab: Laya.Sprite3D;

    public constructor(id, curGamePropData: GamePropData) {
        super();
        this.m_nameID = id;
        this.curGamePropData = curGamePropData;
        this.initFui();
    }

    private initFui() {
        this.m_fui = window.gameFacade.ResourceMng.getMainCom(
            CommonConstant._fuiPropMessagePanelPath.Package,
            "PropMessagePanel",
            "PropMessagePanel");

        this.m_fui.getChild("confirmBtn").onClick(this, this.onClose);;
        this.m_fui.y = 0;
        this.propImageRoot = this.m_fui.getChild("propImageRoot").asGroup;
        this.propImage = this.m_fui.getChild("propImage").asLoader;
        this.imageText = this.m_fui.getChild("propImageName").asLabel;

        let ratio = Laya.Browser.clientWidth/GameConfig.width; ///  xxx/750 算出相对比例
        let h = Laya.Browser.clientHeight/ratio;
        let ratioH = h/GameConfig.height;

        let targetW = GameConfig.width*ratioH

        this.render3DTexture = this.m_fui.getChild("rawImage").asImage;
        this.render3DTexture.width = targetW;
        this.render3DTexture.height = h;

        this.render3DTexture.x = 0;
        this.render3DTexture.y = 0;
        
        this.renderText = this.m_fui.getChild("rawImageText").asLabel;
        this.mowner = this.m_fui.displayObject;
        this.mowner.alpha = 0;
    }

    public InitUi(propData:GamePropData): void {
        this.curGamePropData = propData;
        /** 如果需要显示3D*/
        if (this.curGamePropData.isShow3D) {
            this.propImageRoot.visible = false;
            this.renderText.text = window.gameFacade.ConfigMng.GetLanguageByKey(this.curGamePropData._nameKey);
            /** 生成渲染相机*/
            window.gameFacade.ResourceMng.LoadSprite3D<Laya.Sprite3D>(
                CommonConstant._renderCameraPrefab,
                this.onRenderCameraPrefabLoadSucc, this);
        }
        else {
            this.render3DTexture.visible = false;
            this.renderText.visible = false;
            this.imageText.text = window.gameFacade.ConfigMng.GetLanguageByKey(this.curGamePropData._nameKey);
            window.gameFacade.AtlasMng.SetSpriteByName(this.propImage, this.curGamePropData.atlasName, this.curGamePropData.spriteName);
            Laya.Tween.to(this.mowner,{alpha:1},500);
        }

        super.Show();
    }

    //加载RenderCamera成功
    private onRenderCameraPrefabLoadSucc(renderCameraPrefab: Laya.Sprite3D): void {
        console.log("onRenderCameraPrefabLoadSucc");
        this.renderCameraPrefab = renderCameraPrefab;
        let renderCameraRoot = renderCameraPrefab.getChildAt(0) as Laya.Sprite3D;

        let rendererGameProp = utils.CommonUtils.CustomAddComponent(renderCameraRoot, RendererGameProp) as RendererGameProp;
        rendererGameProp.SetData(this.curGamePropData);

        let renderTargetCamera = renderCameraRoot.getChildAt(0).getChildByName("RenderCamera") as Laya.Camera;
        //选择渲染目标为纹理
        let ratio = Laya.Browser.clientWidth/GameConfig.width; ///  xxx/750 算出相对比例
        let h = Laya.Browser.clientHeight/ratio;
        let ratioH = h/GameConfig.height;

        let targetW = Laya.stage.width*ratioH
        renderTargetCamera.renderTarget = new Laya.RenderTexture(GameConfig.width, h);
        //渲染顺序
        renderTargetCamera.renderingOrder = 1;
        renderTargetCamera.clearColor = new Laya.Vector4(0.2,0.2,0.2,0.01);
        renderTargetCamera.cullingMask = Math.pow(2,9);
        console.log("renderTargetCamera.cullingMask:", renderTargetCamera.cullingMask);

        //清除标记
        renderTargetCamera.clearFlag = Laya.BaseCamera.CLEARFLAG_SOLIDCOLOR;
        var rtex = new Laya.Texture(((<Laya.Texture2D>(renderTargetCamera.renderTarget as any))), Laya.Texture.DEF_UV);
       
        renderTargetCamera.addComponent(CameraRayLogic);
        this.render3DTexture.displayObject.graphics.drawTexture(rtex);
        // this.render3DTexture.blendMode =Laya.BlendMode.MASK;
       
        let scene = window.gameFacade.SceneMng.CurrentScene3D;
        scene.addChild(renderCameraPrefab);

        Laya.Tween.to(this.mowner,{alpha:1},500);
    }

    private onClose() {
        if(this.renderCameraPrefab != null){
            this.renderCameraPrefab.destroy();
        }
        
        window.gameFacade.SceneMng.close(EnumData.EnumPanelType.PropMessagePanel);
    }
}
